﻿//-----------------------------------------------------------------------
// <copyright file="BusinessManager.cs" company="FastNET Services">
//     Copyright (c) 2007-2013. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using FastNet.BusinessSystem.Exceptions;
using FastNet.BusinessSystem.Interfaces;
using FastNet.BusinessSystem.Properties;
using FastNet.RepositorySystem;
using FastNet.RepositorySystem.Interfaces;

#endregion

namespace FastNet.BusinessSystem.Managers
{
    /// <summary>
    /// Base template class for all business object managers to be based off of. All base business logic is contained
    /// within this class for basic Create, Retrieve, Update, and Delete activity for business objects.
    /// </summary>
    /// <typeparam name="TBusiness">
    /// Type of business object that this manager class will be managing. This must be a class and based off of <see cref="IBusinessObject"/>
    /// </typeparam>
    /// <typeparam name="TRepository">
    /// Type of repository that this manager class will use for all the Create, Retrieve, Update, and Delete activity. This must be a class and based off of <see cref="IRepository{T}"/>
    /// </typeparam>
    /// <typeparam name="TData">
    /// Type of the data that the business object is based off of. This must be a class and based off of <see cref="IData"/>
    /// </typeparam>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1005:AvoidExcessiveParametersOnGenericTypes")]
    public abstract class IdentityBusinessManager<TBusiness, TRepository, TData> : BusinessManager<TBusiness, TRepository, TData>
        where TBusiness : class, IBusinessObject
        where TRepository : class, IIdentityRepository<TData>
        where TData : class, IIdentityData
    {
        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityBusinessManager{TBusiness,TRepository,TData}"/> class
        /// </summary>
        /// <param name="factory">
        /// Reference to the <see cref="IRepositoryFactory"/> used for managing the data store provider
        /// </param>
        /// <exception cref="BusinessInitializationException">
        /// Thrown when there was an exception getting a repository or if the repository returned by the factory is null 
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if the passed in factory is null
        /// </exception>
        protected IdentityBusinessManager(IRepositoryFactory factory)
            : base(factory)
        {

        }

        #endregion

        /// <summary>
        /// Retrieves all the business objects for <typeparamref name="TBusiness"/> from the store
        /// </summary>
        /// <param name="id">Identity of the business object to retrieve</param>
        /// <returns>List of all objects for <typeparamref name="TBusiness"/></returns>
        /// <exception cref="BusinessRetrieveException">Thrown if there was an exception during retrieving. Look at the InnerException for detail.</exception>
        public TBusiness Retrieve(Identity id)
        {
            try
            {
                return ConvertData(Repository.Retrieve(id));
            }
            catch (Exception e)
            {
                throw new BusinessRetrieveException(Resources.BusinessManager_RetrieveError, e);
            }
        }
    }
}
